<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
                      <a-col :span="24">
              <a-form-item label="区划编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="['code', validatorRules.code]"
                         placeholder="请输入区划编码"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="成功标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="['success', validatorRules.success]"
                         placeholder="请输入成功标识"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="成功数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="['successCount', validatorRules.successCount]"
                         placeholder="请输入成功数量"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="日志信息" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="['message', validatorRules.message]"
                         placeholder="请输入日志信息"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="['creatTime', validatorRules.creatTime]"
                         placeholder="请输入创建时间"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="日志类型 1备份日志 2网格数据操作日志 3poi数据新增日志" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="['type', validatorRules.type]"
                         placeholder="请输入日志类型 1备份日志 2网格数据操作日志 3poi数据新增日志"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-decorator="['content', validatorRules.content]"
                         placeholder="请输入内容"></a-input>
              </a-form-item>
            </a-col>
        <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
          <a-button @click="submitForm">提 交</a-button>
        </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import {getAction, httpAction} from '@/api/manage'
import pick from 'lodash.pick'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JSwitch from '@/components/jeecg/JSwitch'

export default {
  name: 'PoiUpdateLogForm',
  components: {
    JFormContainer,
    JSwitch,
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      validatorRules: {
        id: {
          rules: [
            {required: false, message: '请输入主键!'}
          ]
        },
        code: {
          rules: [
            {required: false, message: '请输入区划编码!'}
          ]
        },
        success: {
          rules: [
            {required: false, message: '请输入成功标识!'}
          ]
        },
        successCount: {
          rules: [
            {required: false, message: '请输入成功数量!'}
          ]
        },
        message: {
          rules: [
            {required: false, message: '请输入日志信息!'}
          ]
        },
        creatTime: {
          rules: [
            {required: false, message: '请输入创建时间!'}
          ]
        },
        type: {
          rules: [
            {required: false, message: '请输入日志类型 1备份日志 2网格数据操作日志 3poi数据新增日志!'}
          ]
        },
        content: {
          rules: [
            {required: false, message: '请输入内容!'}
          ]
        },
      },
      url: {
        add: "/admin/scenes/poi-update-log/add",
        edit: "/admin/scenes/poi-update-log/edit",
        queryById: "/admin/scenes/poi-update-log/queryById"
      },
      fields: ['id', 'code', 'success', 'successCount', 'message', 'creatTime', 'type', 'content']
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = {id: this.formData.dataId};
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result);
          }
        });
      }
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'post';
          }
          let formData = Object.assign(this.model, values);
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    },
  }
}
</script>
